/**
 * COPYRIGHT HangZhou Trustway Technology Company Limited
 * All right reserved.
 */
package com.robam.cloud.authcenter.service.impl;

import com.robam.cloud.authcenter.dao.AuthPlatformMapper;
import com.robam.cloud.authcenter.helper.ThirdAuthGateway;
import com.robam.cloud.authcenter.manager.ThirdAuthManager;
import com.robam.cloud.authcenter.service.FaceService;
import com.robam.cloud.common.bean.apicenter.entity.SysConfigWithBLOBs;
import com.robam.cloud.common.bean.authcenter.dto.FaceParam;
import com.robam.cloud.common.bean.authcenter.entity.AuthPlatform;
import com.robam.cloud.common.bean.authcenter.entity.FaceRes;
import com.robam.cloud.common.bean.authcenter.enumspace.AuthPlatformTypeEnum;
import com.robam.cloud.common.bean.common.Constant;
import com.robam.cloud.common.bean.common.SysConfigConstant;
import com.robam.cloud.common.bean.restful.entity.ServiceException;
import com.robam.cloud.common.bean.usercenter.entity.LoginAccount;
import com.robam.cloud.common.bean.usercenter.entity.User;
import com.robam.cloud.restful.config.basefeign.FeignUserCenterBaseUtil;
import com.robam.cloud.restful.config.initdata.CurrentAccountAndUserData;
import com.robam.cloud.restful.config.initdata.SysConfigData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2020-07-26 16:21
 * @Version 1.0
 */
@Service
public class FaceServiceImpl implements FaceService {
    @Autowired
    FeignUserCenterBaseUtil feignUserCenterBaseUtil;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    AuthPlatformMapper authPlatformMapper;
    @Autowired
    ThirdAuthManager thirdAuthManager;
    @Autowired
    SysConfigData sysConfigData;
    @Autowired
    CurrentAccountAndUserData currentAccountAndUserData;

    @Override
    public FaceParam getFaceInfo(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader(Constant.TOKENNAME);
        User user = currentAccountAndUserData.getCurrentUser(httpServletRequest);
        LoginAccount loginAccount = currentAccountAndUserData.getCurrentLoginAccount(httpServletRequest);
        return getFaceInfoBySfzmhm(loginAccount, user.getSfzmhm(), user.getSfzmmc(), user.getXm(), Constant.SELF, token);
    }

    @Override
    public FaceParam getOtherFaceInfo(HttpServletRequest httpServletRequest, String certNo, String certType, String certName) {
        String token = httpServletRequest.getHeader(Constant.TOKENNAME);
        LoginAccount loginAccount = currentAccountAndUserData.getCurrentLoginAccount(httpServletRequest);
        return getFaceInfoBySfzmhm(loginAccount, certNo, certType, certName, Constant.NOTSELF, token);
    }

    private FaceParam getFaceInfoBySfzmhm(LoginAccount loginAccount, String certNo, String certType, String certName, String sfbr, String token) {
        AuthPlatform authPlatform = this.authPlatformMapper.selectByPrimaryKey(loginAccount.getPlatformId());
        AuthPlatform faceAuthplatform = this.authPlatformMapper.selectByUuid(authPlatform.getFaceUuid());
        ThirdAuthGateway thirdAuthGateway = thirdAuthManager.call(AuthPlatformTypeEnum.getAuthPlatformTypeEnumByCode(faceAuthplatform.getPaltformType()));
        SysConfigWithBLOBs sysConfigByCsdm = sysConfigData.getSysConfigByCsz(SysConfigConstant.SLYXQ);
        if (sysConfigByCsdm == null) {
            throw new ServiceException("未配扫脸有效期");
        }
        String expireTime = sysConfigByCsdm.getCsz();
        return thirdAuthGateway.getFaceParam(faceAuthplatform, certNo, certType, certName, sfbr, expireTime, token);
    }

    @Override
    public void verifyFaceRsult(HttpServletRequest httpServletRequest, FaceRes faceRes) {
        String token = httpServletRequest.getHeader(Constant.TOKENNAME);
        LoginAccount loginAccount = currentAccountAndUserData.getCurrentLoginAccount(httpServletRequest);
        AuthPlatform authPlatform = this.authPlatformMapper.selectByPrimaryKey(loginAccount.getPlatformId());
        AuthPlatform faceAuthplatform = this.authPlatformMapper.selectByUuid(authPlatform.getFaceUuid());
        ThirdAuthGateway thirdAuthGateway = thirdAuthManager.call(AuthPlatformTypeEnum.getAuthPlatformTypeEnumByCode(faceAuthplatform.getPaltformType()));
        thirdAuthGateway.verifyFaceResult(faceAuthplatform, faceRes, token);
    }
}
